package com.shop.mall.controller;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.shop.mall.common.Result;
import com.shop.mall.entity.Order;
import com.shop.mall.entity.Product;
import com.shop.mall.service.OrderService;
import com.shop.mall.service.ProductService;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import java.math.BigDecimal;

/**
 * <p>
 * 订单 前端控制器
 * </p>
 */
@RestController
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private ProductService productService;
    @Autowired
    private OrderService orderService;


    @PostMapping("/add")
    @Operation(summary = "添加订单")
    public Result<String> add(@RequestBody Order order) {
        String code = order.getCode();
        LambdaQueryWrapper<Order> wr = new LambdaQueryWrapper<>();
        wr.eq(Order::getCode,code);
        Order one = orderService.getOne(wr);
        if (one !=null){
            throw new RuntimeException("订单编号重复");
        }
        order.setStatus("待支付");
        Product byId = productService.getById(order.getProId());
        if (byId == null){
            throw new RuntimeException("未找到该商品");
        }
        order.setName(byId.getName());
        order.setPrice(byId.getPrice());
        order.setTotal(order.getPrice().multiply(new BigDecimal(order.getNum())));
        boolean result = orderService.save(order);
        if (!result) {
            return Result.error("添加失败");
        }
        return Result.success();
    }


    @GetMapping("/getById")
    @Operation(summary = "根据id查询订单信息")
    public Result<Order> getById(@RequestParam int id) {
        Order byId = orderService.getById(id);
        if (byId == null) {
            return Result.error("获取订单信息失败");
        }
        return Result.success(byId);
    }


    @PutMapping("/update")
    @Operation(summary = "修改订单")
    public Result<String> update(@RequestBody Order order) {
        Integer id = order.getId();
        Order order1 = orderService.getById(id);
        if (order1 == null){
            throw new RuntimeException("未找到该订单");
        }
        Product product = productService.getById(order.getProId());
        if (product == null){
            throw new RuntimeException("未找到该商品");
        }
        order.setName(product.getName());
        order.setPrice(product.getPrice());
        order.setTotal(order.getPrice().multiply(new BigDecimal(order.getNum())));
        boolean result = orderService.updateById(order);
        if (!result) {
            return Result.error("修改失败");
        }
        String status = order.getStatus();
        if ("已发货".equals(status)){
            int left = product.getNums() - order.getNum();
            product.setNums(left);
            productService.updateById(product);
        }
        return Result.success();
    }

    /**
     * 删除
     */
    @DeleteMapping("/delete/{id}")
    @Operation(summary = "删除订单")
    public Result<String> delete(@PathVariable int id) {
        boolean result = orderService.removeById(id);
        if (!result) {
            return Result.error("删除失败");
        }
        return Result.success();
    }
}
